
package XMLStuff;

import JET.Centers.ControlCenter;
import JET.Centers.Scripts.MyScript;
import JET.Frames.DataModels.DataModelsFactory;
import JET.Scene;
import JET.StaticActions;
import JET.physics.MaterialPair;
import org.w3c.dom.*;

/**
 *
 * @author 057Ry
 */
public class XMLControlCenterHelper {

    static final ControlCenter CC = ControlCenter.getInstance();

    public static void loadMaterialPairs(Element materialPairs, Scene scene)
    {
        int count = 1;

        // Load attributes
        NamedNodeMap attributes = materialPairs.getAttributes();
        for(int i=0; i<attributes.getLength(); i++)
        {
            Node attr = attributes.item(i);

            if (attr.getNodeName().equals("count"))
            {
                //getEngine().materialsCount = Integer.parseInt(attr.getNodeValue());
                count = Integer.parseInt(attr.getNodeValue());
                scene.getJETContext().materials = new MaterialPair[count][count];
                //System.out.println("materials count "+count);
            }
        }

        int iter=0;
        NodeList materialPairList = materialPairs.getChildNodes();
        for(int m=0; m<materialPairList.getLength(); m++)
        {
           if (materialPairList.item(m) instanceof Element)
           {
                Element materialPairListEl = (Element)materialPairList.item(m);
                if (materialPairListEl.getTagName().equals("materialPair"))
                {
                    int row = iter/count;
                    int col = iter%count;
                    iter++;
                    //System.out.println("iter "+iter+" count "+count+" row "+row+" col "+col);

                    scene.getJETContext().materials[row][col] = new MaterialPair(materialPairListEl);
                }
           }
        }
    }

    public static void loadMaterialNames(Element materialNames, Scene scene)
    {
        // Load attributes
        NamedNodeMap attributes = materialNames.getAttributes();
        for(int i=0; i<attributes.getLength(); i++)
        {
            Node attr = attributes.item(i);

            if (attr.getNodeName().equals("count"))
            {
                scene.getJETContext().materialsCount = Integer.parseInt(attr.getNodeValue());
                scene.getJETContext().materialNames = new String[scene.getJETContext().materialsCount];

                System.out.println("materials names count "+scene.getJETContext().materialsCount);
            }
        }

        int iter=0;
        NodeList materialNamesList = materialNames.getChildNodes();
        for(int m=0; m<materialNamesList.getLength(); m++)
        {
           if (materialNamesList.item(m) instanceof Element)
           {
                Element element = (Element)materialNamesList.item(m);
                if (element.getTagName().equals("materialName"))
                {
                    Text textNode = (Text) element.getFirstChild();
                    String text = textNode.getData().trim();
                    scene.getJETContext().materialNames[iter++] = text;
                }
           }
        }
        if (CC.GUIC.materialsDialog != null)
            CC.GUIC.materialsDialog.updateTable();
    }

    public static void loadScripts(Element scriptsElem) {
        int count = Integer.parseInt(scriptsElem.getAttribute("count"));

        NodeList scriptsNodes = scriptsElem.getChildNodes();
        for(int i=0; i<scriptsNodes.getLength(); i++)
            if (scriptsNodes.item(i) instanceof Element) {
                Element scriptElem = (Element) scriptsNodes.item(i);

                if (scriptElem.getTagName().equals("script")) {
                    String name = scriptElem.getAttribute("name");
                    boolean edited = Boolean.parseBoolean( scriptElem.getAttribute("edited"));

                    String text = scriptElem.getFirstChild().getTextContent();

                    MyScript script = new MyScript(name);
                    script.setScript(text);
                    // At this moment all scripts are not edited.
                    // Edited state of this script may change if gui loader finds CPScriptEditor elements of this script...
                    script.setEdited(false);

                    //scene.SC.addScript(script);
                    DataModelsFactory.getScriptsListModel().fireScriptsListChanged();
                }
            }
    }

    public static void loadVirtualKeyboard(Element vKeyboardEl, Scene scene)
    {
        NodeList vKeyboardElNodeList = vKeyboardEl.getChildNodes();
        for(int i=0; i<vKeyboardElNodeList.getLength(); i++)
           if (vKeyboardElNodeList.item(i) instanceof Element)
           {
                Element vKeyEl = (Element)vKeyboardElNodeList.item(i);
                if (vKeyEl.getTagName().equals("Vkey_Actions"));
                {
                    int keyCode = -1;
                    NodeList vKeyElNodeList = vKeyEl.getChildNodes();
                    for(int a=0; a<vKeyElNodeList.getLength(); a++)
                        if (vKeyElNodeList.item(a) instanceof Element)
                        {
                            Element elem = (Element)vKeyElNodeList.item(a);
                            if (elem.getTagName().equals("VKey_Code"))
                            {
                                Text textNode = (Text) elem.getFirstChild();
                                String text = textNode.getData().trim();
                                keyCode = Integer.parseInt(text);
                            }
                            else if (elem.getTagName().equals("Press_Actions_Indices"))
                            {
                                NodeList pressActionsElNodeList = elem.getChildNodes();
                                for(int p=0; p<pressActionsElNodeList.getLength(); p++)
                                    if (pressActionsElNodeList.item(p) instanceof Element)
                                    {
                                        Element pressActionEl = (Element)pressActionsElNodeList.item(p);
                                        if (pressActionEl.getTagName().equals("Action_Index"))
                                        {
                                            Text textNode = (Text) pressActionEl.getFirstChild();
                                            String text = textNode.getData().trim();
                                            int aIndex = Integer.parseInt(text);
                                            StaticActions.bindActionToKeyPressActions(keyCode, aIndex, scene);
                                        }
                                    }
                            }
                            else if (elem.getTagName().equals("Release_Actions_Indices"))
                            {
                                NodeList releaseActionsElNodeList = elem.getChildNodes();
                                for(int r=0; r<releaseActionsElNodeList.getLength(); r++)
                                    if (releaseActionsElNodeList.item(r) instanceof Element)
                                    {
                                        Element releaseActionEl = (Element)releaseActionsElNodeList.item(r);
                                        if (releaseActionEl.getTagName().equals("Action_Index"))
                                        {
                                            Text textNode = (Text) releaseActionEl.getFirstChild();
                                            String text = textNode.getData().trim();
                                            int aIndex = Integer.parseInt(text);
                                            StaticActions.bindActionToKeyReleaseActions(keyCode, aIndex, scene);
                                        }
                                    }
                            }
                        }
                }
           }
    }
}
